package com.jxltech.unicam;

import com.jxltech.unicam.ui.JBootstrapFactory;
import com.jxltech.unicam.utils.AudioPlayer;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.font.TextAttribute;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

/**
 * Dialog with info about UniCam
 *
 * @author Jelmerro
 */
public class AboutDialog extends JDialog {

    private final Box box;

    /**
     * Constructor for the dialog
     */
    public AboutDialog() {
        //Call super method
        super(Frame.getInstance(), "关于系统", ModalityType.APPLICATION_MODAL);
        //Set properties
        setResizable(false);
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(screen.width / 2 - 175, screen.height / 2 - 125);
        setSize(450, 180);
        //Add items
        box = new PictureBox(BoxLayout.Y_AXIS);
        addLabelToBox("烟箱识别系统1.0", 50, null);
//        addLabelToBox("烟箱自动识别系统", 12, null);
        addLabelToBox("山西省晋城市烟草局", 16, null);
        addLabelToBox("版权所有", 16, null);
        add(box);
    }

    /**
     * Creates and adds a label to the box
     *
     * @param text String
     * @param size int
     * @param url String
     */
    private void addLabelToBox(String text, int size, final String url) {
        JLabel label = new JLabel(text);
        label.setAlignmentX(Component.CENTER_ALIGNMENT);
//        label.setFont(new Font("Arial", Font.PLAIN, size));
        label.setBorder(new EmptyBorder(10, 10, 10, 10));
        //If an url is provided, color it, underline it and make it clickable
        if (url != null) {
            label.setForeground(Color.BLUE);
            label.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    try {
                        Desktop.getDesktop().browse(new URI(url));
                    } catch (Exception ex) {
                        JTextField textField = new JTextField(url);
                        textField.setEditable(false);
                        JOptionPane.showMessageDialog(null, textField, "网站链接", JOptionPane.INFORMATION_MESSAGE);
                    }
                }
            });
            Font font = label.getFont();
            Map attributes = font.getAttributes();
            attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
            label.setFont(font.deriveFont(attributes));
        }
        box.add(label);
    }

    private class PictureBox extends Box {
        private Image img;

        public PictureBox(int axis) {
            super(axis);
            Image img = null;
            try {
                InputStream inStream = AboutDialog.class.getClassLoader().getResourceAsStream("bg.jpg");
                img = ImageIO.read(inStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
            this.img = img;
        }

        public Color getForeground() {
            return Color.RED;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (img != null) {
                Graphics2D g2 = (Graphics2D) g.create();
                g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .4f));
                g2.drawImage(img, 0, 0, getWidth(), getHeight(), this);
                g2.dispose();
            }
        }
    }
}
